<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>收货地址 - 商品管理系统</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/fontawesome-all.min.css">
    <link rel="stylesheet" href="/css/style.css">
    <style>
        .address-card {
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
            transition: all 0.3s;
        }
        
        .address-card:hover {
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        
        .address-card.default {
            border-color: #4CAF50;
            background-color: #f9fff9;
        }
        
        .default-badge {
            background-color: #4CAF50;
            color: white;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
            margin-left: 10px;
        }
        
        .address-actions {
            margin-top: 10px;
        }
        
        .add-address-btn {
            width: 100%;
            padding: 15px;
            border: 2px dashed #ccc;
            border-radius: 8px;
            background-color: transparent;
            color: #666;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .add-address-btn:hover {
            border-color: #4CAF50;
            color: #4CAF50;
            background-color: #f9fff9;
        }
        
        .address-form {
            display: none;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 15px;
        }
        
        .form-row {
            display: flex;
            gap: 15px;
            margin-bottom: 15px;
        }
        
        .form-row .form-group {
            flex: 1;
        }
    </style>
</head>
<body>
    <!-- 顶部导航 -->
    <header th:replace="shop/fragments/header :: header"></header>
    
    <div class="container mt-4">
        <div class="row">
            <!-- 左侧菜单 -->
            <div class="col-md-3">
                <div th:replace="shop/fragments/profile-sidebar :: profile-sidebar"></div>
            </div>
            
            <!-- 右侧内容 -->
            <div class="col-md-9">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h4 class="mb-0">收货地址</h4>
                        <button class="btn btn-success" id="toggleAddForm">
                            <i class="fas fa-plus"></i> 添加地址
                        </button>
                    </div>
                    <div class="card-body">
                        <!-- 添加地址表单 -->
                        <div class="address-form" id="addAddressForm">
                            <h5>添加新地址</h5>
                            <form id="addressForm">
                                <input type="hidden" id="addressId" name="id">
                                <div class="form-row">
                                    <div class="form-group">
                                        <label for="name">收货人</label>
                                        <input type="text" class="form-control" id="name" name="name" required>
                                    </div>
                                    <div class="form-group">
                                        <label for="phone">手机号码</label>
                                        <input type="tel" class="form-control" id="phone" name="phone" required>
                                    </div>
                                </div>
                                <div class="form-row">
                                    <div class="form-group">
                                        <label for="province">省份</label>
                                        <input type="text" class="form-control" id="province" name="province" required>
                                    </div>
                                    <div class="form-group">
                                        <label for="city">城市</label>
                                        <input type="text" class="form-control" id="city" name="city" required>
                                    </div>
                                    <div class="form-group">
                                        <label for="district">区/县</label>
                                        <input type="text" class="form-control" id="district" name="district" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="detail">详细地址</label>
                                    <textarea class="form-control" id="detail" name="detail" rows="2" required></textarea>
                                </div>
                                <div class="form-group">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="isDefault" name="isDefault">
                                        <label class="form-check-label" for="isDefault">
                                            设为默认地址
                                        </label>
                                    </div>
                                </div>
                                <div class="d-flex justify-content-between">
                                    <button type="button" class="btn btn-secondary" id="cancelEdit">取消</button>
                                    <button type="submit" class="btn btn-primary" id="saveAddress">保存地址</button>
                                </div>
                            </form>
                        </div>
                        
                        <!-- 地址列表 -->
                        <div id="addressList">
                            <div th:if="${#lists.isEmpty(addresses)}" class="text-center py-4">
                                <i class="fas fa-map-marker-alt fa-3x text-muted mb-3"></i>
                                <p class="text-muted">您还没有添加收货地址</p>
                            </div>
                            
                            <div th:each="address : ${addresses}" class="address-card" th:classappend="${address.isDefault} ? 'default' : ''">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div class="address-info">
                                        <h5>
                                            <span th:text="${address.name}">收货人</span>
                                            <span th:if="${address.isDefault}" class="default-badge">默认</span>
                                        </h5>
                                        <p class="mb-1"><i class="fas fa-phone"></i> <span th:text="${address.phone}">手机号</span></p>
                                        <p class="mb-0">
                                            <i class="fas fa-map-marker-alt"></i> 
                                            <span th:text="${address.province}">省份</span>
                                            <span th:text="${address.city}">城市</span>
                                            <span th:text="${address.district}">区县</span>
                                            <span th:text="${address.detail}">详细地址</span>
                                        </p>
                                    </div>
                                    <div class="address-actions">
                                        <button class="btn btn-sm btn-outline-primary edit-address" th:data-id="${address.id}">
                                            <i class="fas fa-edit"></i> 编辑
                                        </button>
                                        <button class="btn btn-sm btn-outline-danger delete-address" th:data-id="${address.id}">
                                            <i class="fas fa-trash"></i> 删除
                                        </button>
                                        <button th:if="${!address.isDefault}" class="btn btn-sm btn-outline-success set-default" th:data-id="${address.id}">
                                            <i class="fas fa-check"></i> 设为默认
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 底部 -->
    <footer th:replace="shop/fragments/footer :: footer"></footer>
    
    <script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.bundle.min.js"></script>
    <script src="/js/toast.js"></script>
    <script>
        $(document).ready(function() {
            // 显示/隐藏添加地址表单
            $('#toggleAddForm').click(function() {
                resetForm();
                $('#addAddressForm').slideToggle();
            });
            
            // 取消编辑
            $('#cancelEdit').click(function() {
                resetForm();
                $('#addAddressForm').slideUp();
            });
            
            // 保存地址
            $('#addressForm').submit(function(e) {
                e.preventDefault();
                
                const addressId = $('#addressId').val();
                const isEdit = addressId !== '';
                
                const formData = {
                    name: $('#name').val(),
                    phone: $('#phone').val(),
                    province: $('#province').val(),
                    city: $('#city').val(),
                    district: $('#district').val(),
                    detail: $('#detail').val(),
                    isDefault: $('#isDefault').is(':checked')
                };
                
                const url = isEdit ? '/address/update' : '/address/add';
                
                if (isEdit) {
                    formData.id = addressId;
                }
                
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: formData,
                    success: function(response) {
                        if (response.success) {
                            showToast(response.message, 'success');
                            setTimeout(() => {
                                location.reload();
                            }, 1000);
                        } else {
                            showToast(response.message, 'error');
                        }
                    },
                    error: function() {
                        showToast('操作失败，请重试', 'error');
                    }
                });
            });
            
            // 编辑地址
            $('.edit-address').click(function() {
                const addressId = $(this).data('id');
                
                $.ajax({
                    url: '/address/get/' + addressId,
                    type: 'GET',
                    success: function(response) {
                        if (response.success) {
                            const address = response.address;
                            
                            $('#addressId').val(address.id);
                            $('#name').val(address.name);
                            $('#phone').val(address.phone);
                            $('#province').val(address.province);
                            $('#city').val(address.city);
                            $('#district').val(address.district);
                            $('#detail').val(address.detail);
                            $('#isDefault').prop('checked', address.isDefault);
                            
                            $('#addAddressForm').slideDown();
                            $('html, body').animate({
                                scrollTop: $('#addAddressForm').offset().top - 100
                            }, 500);
                        } else {
                            showToast(response.message, 'error');
                        }
                    },
                    error: function() {
                        showToast('获取地址信息失败', 'error');
                    }
                });
            });
            
            // 删除地址
            $('.delete-address').click(function() {
                const addressId = $(this).data('id');
                
                if (confirm('确定要删除这个地址吗？')) {
                    $.ajax({
                        url: '/address/delete/' + addressId,
                        type: 'POST',
                        success: function(response) {
                            if (response.success) {
                                showToast(response.message, 'success');
                                setTimeout(() => {
                                    location.reload();
                                }, 1000);
                            } else {
                                showToast(response.message, 'error');
                            }
                        },
                        error: function() {
                            showToast('删除失败，请重试', 'error');
                        }
                    });
                }
            });
            
            // 设置默认地址
            $('.set-default').click(function() {
                const addressId = $(this).data('id');
                
                $.ajax({
                    url: '/address/setDefault/' + addressId,
                    type: 'POST',
                    success: function(response) {
                        if (response.success) {
                            showToast(response.message, 'success');
                            setTimeout(() => {
                                location.reload();
                            }, 1000);
                        } else {
                            showToast(response.message, 'error');
                        }
                    },
                    error: function() {
                        showToast('设置失败，请重试', 'error');
                    }
                });
            });
            
            // 重置表单
            function resetForm() {
                $('#addressForm')[0].reset();
                $('#addressId').val('');
            }
        });
    </script>
</body>
</html>